




function SearchChildByName(node, name)
	if( node==nil or name==nil ) then
		return nil
	end
	if( node:getChildByName(name)~=nil ) then 
		return node:getChildByName(name)
	else 
		local ary = node:getChildren();
		for i, son in pairs(ary) do 
			local one = SearchChildByName(son, name)
			if one~=nil then 
				return one;
			end
		end
	end
	return nil
end



-- 判断世界坐标系的点worldPoint是否处于node内部。
function IsContain(node,worldPoint)
    -- 该判断方法的node，不能是空node，也不能是拉伸过的有纹理node，否则，坐标点转换会出问题。所以最好用layout/sprite。
    if(false)then
        local ptO = node:convertToNodeSpace(cc.p(0,0))
        local recBox = node:getBoundingBox()
        local ptTou = node:convertToNodeSpace(worldPoint)
        recBox.x = 0
        recBox.y = 0
        return cc.rectContainsPoint(recBox,ptTou)
    end
    -- 新的判断方法
    local recBox = node:getBoundingBox() 
    local px,py = node:getPosition()
    local anchor = node:getAnchorPoint()
    px = px - recBox.width * anchor.x
    py = py - recBox.height * anchor.y
    local par = node:getParent() 
    local wod = par:convertToWorldSpaceAR( cc.p(px, py) )   
    recBox.x = wod.x
    recBox.y = wod.y
    return cc.rectContainsPoint( recBox, worldPoint)
end


function IsContainTest(node,worldPoint)
    -- 新的判断方法
    local recBox = node:getBoundingBox() 
    local px,py = node:getPosition()
    local anchor = node:getAnchorPoint()
    cclook(" px:%s  py:%s", px, py )
    cclook(" recBox:%s ", recBox )
    cclook(" worldPoint:%s ", worldPoint )
    px = px - recBox.width * anchor.x
    py = py - recBox.height * anchor.y

    cclook(" px:%s  py:%s", px, py )
    local par = node:getParent() 
    local wod = par:convertToWorldSpaceAR( cc.p(px, py) )   
    recBox.x = wod.x
    recBox.y = wod.y
    cclook(" recBox:%s \n\n\n", recBox )
    return cc.rectContainsPoint( recBox, worldPoint)
end



function AddTouchEvent( node, clickFunc, checkEndNode, touchOnEvt, touchMoveEvt, touchCancelEvt )

    if node==nil then
        return nil;
    end
    -- 
    local DefScaleX = node:getScaleX();
    local DefScaleY = node:getScaleY();

    -- 创建单点触摸事件监听器  
    local function onTouchBegan( touch, event ) 
        local inside = false;
        local pt = touch:getLocation();
        local pTouch = node:convertTouchToNodeSpaceAR(touch);  -- 转换 触摸 到 节点空间
        if ( IsContain( node,pt ) ) then
            node:setScaleX(DefScaleX*1.1);
            node:setScaleY(DefScaleY*1.1);
            inside = true;
        end
        if (inside==false) then  return false end
        -- 要检查一下父 node 遍历上去，是不是隐藏了。
        if( node:isVisible()==false) then return false end
        local checkParentNode = node;
        for i=1,10000 do
            checkParentNode = checkParentNode:getParent()
            if (checkParentNode:isVisible()==false ) then
                cclook("有 父级隐藏的: %s", node:getName() )
                return false;
            end
            -- 检查边界。 
            if( checkParentNode==checkEndNode )then
                break;
            end
        end
        -- 给这个Node 添加一个存放信息的空Node.
        local checkMoveNode = node:getChildByName("SpriteTouch");
        if ( checkMoveNode==nil ) then
            checkMoveNode = cc.Node:create();
            node:addChild(checkMoveNode);
            checkMoveNode:setName("SpriteTouch");
        end
        checkMoveNode:setPosition(touch:getLocationInView() );
        checkMoveNode:setTag(1);
        if( touchOnEvt ) then
            touchOnEvt( node, touch )
        end
        return true;
    end

    local function onTouchMoved( touch, event ) 
        local checkMoveNode = node:getChildByName("SpriteTouch");
        local nowPoi = touch:getLocationInView();
        local startPoi = cc.p( checkMoveNode:getPosition() );
        -- 在scrollView上的触摸点移动了超过10个像素时，就算滑动了，就不能再算作点击了。
        if ( distance(startPoi, nowPoi)>100) then
            node:setTag(-1);
        end
        if( touchMoveEvt ) then
            touchMoveEvt( node, touch )
        end
    end

    local function onTouchEnded( touch, event ) 
        node:setScaleX(DefScaleX);
        node:setScaleY(DefScaleY);
        -- 还要判断是否移动了的事件。
        local checkNode = node:getChildByName("SpriteTouch");
        if( IsContain(node, touch:getLocation())==false )then
            if(touchCancelEvt) then
                touchCancelEvt(node, touch)
            end
        	return nil
        end
        if ( clickFunc~=nil and checkNode:getTag() ~= -1 ) then
            clickFunc( node, touch );
        end
    end
    -- 
    local listener = cc.EventListenerTouchOneByOne:create()
    listener:setSwallowTouches( false )
    listener:registerScriptHandler( onTouchBegan , cc.Handler.EVENT_TOUCH_BEGAN )  
    listener:registerScriptHandler( onTouchMoved , cc.Handler.EVENT_TOUCH_MOVED )
    listener:registerScriptHandler( onTouchEnded , cc.Handler.EVENT_TOUCH_ENDED ) 
    node:getEventDispatcher():addEventListenerWithSceneGraphPriority( listener, node )
end






function AddTouchEventTest( node, clickFunc, checkEndNode, touchOnEvt, touchMoveEvt, touchCancelEvt )

    if node==nil then
        return nil;
    end
    -- 
    local DefScaleX = node:getScaleX();
    local DefScaleY = node:getScaleY();

    -- 创建单点触摸事件监听器  
    local function onTouchBegan( touch, event ) 
        local inside = false;
        local pt = touch:getLocation();
        local pTouch = node:convertTouchToNodeSpaceAR(touch);  -- 转换 触摸 到 节点空间
        cclog(" ontegan ");
        if ( IsContainTest( node,pt ) ) then
            node:setScaleX(DefScaleX*1.1);
            node:setScaleY(DefScaleY*1.1);
            inside = true;
            cclog(" ontegan %s",inside);
        end
        cclog(" inside %s",inside);
        if (inside==false) then  return false end

        -- 要检查一下父 node 遍历上去，是不是隐藏了。
        if( node:isVisible()==false) then return false end
        local checkParentNode = node;
        for i=1,10000 do
            checkParentNode = checkParentNode:getParent()
            if (checkParentNode:isVisible()==false ) then
                cclook("有 父级隐藏的: %s", node:getName() )
                return false;
            end
            -- 检查边界。 
            if( checkParentNode==checkEndNode )then
                cclog(" ontegan %s",inside);
                break;
            end
        end
        -- 给这个Node 添加一个存放信息的空Node.
        local checkMoveNode = node:getChildByName("SpriteTouch");
        if ( checkMoveNode==nil ) then
            checkMoveNode = cc.Node:create();
            node:addChild(checkMoveNode);
            checkMoveNode:setName("SpriteTouch");
        end
        checkMoveNode:setPosition(touch:getLocationInView() );
        checkMoveNode:setTag(1);
        if( touchOnEvt ) then
            touchOnEvt( node, touch )
        end
        return true;
    end

    local function onTouchMoved( touch, event ) 
        local checkMoveNode = node:getChildByName("SpriteTouch");
        local nowPoi = touch:getLocationInView();
        local startPoi = cc.p( checkMoveNode:getPosition() );
        -- 在scrollView上的触摸点移动了超过10个像素时，就算滑动了，就不能再算作点击了。
        if ( distance(startPoi, nowPoi)>100) then
            node:setTag(-1);
        end
        cclog(" move ");
        if( touchMoveEvt ) then
            touchMoveEvt( node, touch )
        end
    end

    local function onTouchEnded( touch, event ) 
        node:setScaleX(DefScaleX);
        node:setScaleY(DefScaleY);
        -- 还要判断是否移动了的事件。
        local checkNode = node:getChildByName("SpriteTouch");
        if( IsContain(node, touch:getLocation())==false )then
            if(touchCancelEvt) then
                touchCancelEvt(node, touch)
            end

            cclog(" end==cancel ");
            return nil
        end
        if ( clickFunc~=nil and checkNode:getTag() ~= -1 ) then
            clickFunc( node, touch );
        end
    end
    -- 
    local listener = cc.EventListenerTouchOneByOne:create()
    listener:setSwallowTouches( false )
    listener:registerScriptHandler( onTouchBegan , cc.Handler.EVENT_TOUCH_BEGAN )  
    listener:registerScriptHandler( onTouchMoved , cc.Handler.EVENT_TOUCH_MOVED )
    listener:registerScriptHandler( onTouchEnded , cc.Handler.EVENT_TOUCH_ENDED ) 
    node:getEventDispatcher():addEventListenerWithSceneGraphPriority( listener, node )
end




-- 连续间隔地执行一系列事件
function FuncOneByOne(del, evtTab , onNode)
    local nod = cc.Node:create();
    if( onNode==nil )then
        cc.Director:getInstance():getRunningScene():addChild(nod)
    else
        onNode:addChild(nod)
    end
    -- evt
    local funcNum = getTableCount(evtTab)
    local seqTab = {}
    for i=1,funcNum do 
        local evt = evtTab[i]
        local callFunc = cc.CallFunc:create(evt)
        local delAct = cc.DelayTime:create( del )
        table.insert( seqTab, callFunc )
        table.insert( seqTab, delAct )
    end
    -- remove
    local function endEvt()
        nod:removeFromParent()
    end
    local endCall = cc.CallFunc:create(endEvt)
    table.insert( seqTab, endCall )
    --
    local seq = cc.Sequence:create(seqTab);
    nod:runAction( seq )
end






function Action_blen(node)

end

































